/*
 *  Copyright (C) 2011 JiangHongTiao <jjurco.sk_gmail.com>
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package sk.lieskove301.jianghongtiao.liaad.prisoner.strategy;

/**
 * Date of create: May 15, 2011
 *
 * @author JiangHongTiao <jjurco.sk_gmail.com>
 * @version 2011.0515
 */
public enum FightResultEnum {
    BothCooperate(new Integer(1)),
    CooperateBetray(new Integer(2)),
    BetrayCooperate(new Integer(3)),
    BothBetray(new Integer(4));

    private final Integer value;

    FightResultEnum(Integer value){
        this.value = value;
    }

    public Integer value() {
        return value;
    }

    /**
     * return binary representation of value
     * @return
     * <ul>
     * <li>for BothCooperate   - 3 - 11bin</li>
     * <li>for CooperateBetray - 2 - 10bin</li>
     * <li>for BetrayCooperate - 1 - 01bin</li>
     * <li>for BothBetray      - 0 - 00bin</li>
     * </ul>
     */
    public Integer binValue(){
        return (value*3) & 3;
    }

    public static FightResultEnum fromValue(Integer d) {
        for (FightResultEnum c: FightResultEnum.values()) {
            if (c.value.equals(d)) {
                return c;
            }
        }
        throw new IllegalArgumentException(d.toString());
    }

    public static FightResultEnum fromBinValue(boolean first, boolean second){
        if(first && second) return FightResultEnum.BothCooperate;
        if(first && !second) return FightResultEnum.CooperateBetray;
        if(!first && second) return FightResultEnum.BetrayCooperate;
        if(!first && !second) return FightResultEnum.BothBetray;
        throw new IllegalArgumentException("Bad arguments, shit happened.");
    }
}
